#include <iostream>
#include "UdpServer.hpp"
#include <memory>
#include "Comm.hpp"

#include <stdio.h>
void Usage(std::string proc)
{
    std::cout << "格式：" << proc << " port" << std::endl;
}

std::string OnMessageDemo(std::string request)
{
    return request + ":Demo";
}

std::string ExcuteCommand(std::string command)
{
    std::cout << command << std::endl;
    FILE* fp = popen(command.c_str(), "r");
    if(!fp)
    {
        return "fopen command failed";
    }

    std::string response;
    char buffer[1024];
    while(1)
    {
        char* ptr = fgets(buffer, sizeof buffer, fp);
        if(!ptr) break;
        else response += buffer;
    }
    pclose(fp);
    return response;
}
int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        Usage(argv[0]);
        return usage_err;
    }

    uint16_t port = std::stoi(argv[1]);
    std::unique_ptr<UdpServer> svr = std::make_unique<UdpServer>(ExcuteCommand, port);

    svr->Init();
    svr->Start();

    return 0;
}